[red-knot] Fix disambiguate display for union types#16907
Merged
MichaReiser merged 3 commits intoastral-sh:mainfrom Mar 22, 2025
Merged
[red-knot] Fix disambiguate display for union types#16907MichaReiser merged 3 commits intoastral-sh:mainfrom
MichaReiser merged 3 commits intoastral-sh:mainfrom
Conversation
Contributor
|
Comment on lines
+155
to
+169
| ## Union | ||
|
|
||
| ```py | ||
| from typing import Callable, Union | ||
|
|
||
| def _( | ||
| c: Callable[[Union[int, str]], int] | None, | ||
| d: None | Callable[[Union[int, str]], int], | ||
| e: None | Callable[[Union[int, str]], int] | int, | ||
| ): | ||
| reveal_type(c) # revealed: ((int | str, /) -> int) | None | ||
| reveal_type(d) # revealed: None | ((int | str, /) -> int) | ||
| reveal_type(e) # revealed: None | ((int | str, /) -> int) | int | ||
| ``` | ||
|
|
Contributor
There was a problem hiding this comment.
What about intersection types?
def _(
c: Intersection[Callable[[Union[int, str]], int], A],
d: Intersection[A, Callable[[Union[int, str]], int]],
e: Intersection[A, Callable[[Union[int, str]], int], B],
f: Intersection[Not[Callable[[int, str], Intersection[int, str]]]]
):
reveal_type(c) # revealed: ((int | str, /) -> int) & A
reveal_type(d) # revealed: A & ((int | str, /) -> int)
reveal_type(e) # revealed: A & ((int | str, /) -> int) & B
reveal_type(f) # revealed: ~((int, str, /) -> int & str)
Contributor
Author
There was a problem hiding this comment.
Good point, right now revealed type of f is ~(int, str, /) -> int & str". We can probably do this in another PR?
Member
There was a problem hiding this comment.
Yeah, I think it's fine to do this in another PR.
MichaReiser
reviewed
Mar 22, 2025
MichaReiser
approved these changes
Mar 22, 2025
Co-authored-by: Micha Reiser <micha@reiser.io>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
When callables are displayed in unions, like:
This leaves the type rather ambiguous, to fix this we can add parenthesis to callable type in union
Fixes #16893
Test Plan
Update callable annotations tests